package com.code.wordhelper.model;

import org.jodconverter.core.DocumentConverter;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 缓存容器
 */
public final class CacheManager {
    private static ConcurrentHashMap<String, Object> cacheMap = null;

    private static class CacheManagerHolder {
        private static CacheManager innerCacheManager = new CacheManager();
    }

    private CacheManager() {
        cacheMap = new ConcurrentHashMap<>();
    }

    public Object getValue(String key) {
        return cacheMap.get(key);
    }

    public void setValue(String key, Object value) {
        cacheMap.put(key, value);
    }

    public void remove(String key) {
        cacheMap.remove(key);
    }

    public static CacheManager getInstance() {
        return CacheManagerHolder.innerCacheManager;
    }

    public static void setDocumentConverter(DocumentConverter documentConverter) {
        getInstance().setValue("documentConverter", documentConverter);
    }

    public static DocumentConverter getDocumentConverter() {
        return (DocumentConverter) getInstance().getValue("documentConverter");
    }

    public static void main(String[] args) {
        CacheManager cacheManager = CacheManager.getInstance();
        cacheManager.setValue("name", "aa");
        cacheManager.setValue("age", "23");
        System.out.println(cacheManager.getValue("name"));
        System.out.println(cacheManager.getValue("age"));

        CacheManager cacheManager2 = CacheManager.getInstance();
        System.out.println(cacheManager2.getValue("name"));
        System.out.println(cacheManager2.getValue("age"));
        System.out.println(cacheManager == cacheManager2);
    }
}
